package org.chen;


import com.zaxxer.hikari.HikariDataSource;
import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
public class ShardingSphereJdbcConfiguration {

    @Bean
    public DataSource configure() throws SQLException {
        //配置真实数据源
        Map<String, DataSource> dataSourceMap = new HashMap<String, DataSource>();

        //配置1个数据源
        HikariDataSource dataSource1 = new HikariDataSource();
        dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource1.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/geek_ds_0?serverTimezone=UTC&useSSL=false");
        dataSource1.setUsername("root");
        dataSource1.setPassword("c123456");
        dataSourceMap.put("ds_0", dataSource1);

        //配置第2个数据源
        HikariDataSource dataSource2 = new HikariDataSource();
        dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource2.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/geek_ds_1?serverTimezone=UTC&useSSL=false");
        dataSource2.setUsername("root");
        dataSource2.setPassword("c123456");
        dataSourceMap.put("ds_1", dataSource2);

        //配置t_order表规则
        ShardingTableRuleConfiguration orderTableRuleConfig = new
                ShardingTableRuleConfiguration("t_order", "ds_${0..1}.t_order_${0..15}");

        //配置分库策略
        orderTableRuleConfig.setDatabaseShardingStrategy(new
                StandardShardingStrategyConfiguration("user_id", "dbShardingAlgorithm"));

        //配置分表策略
        orderTableRuleConfig.setTableShardingStrategy(new
                StandardShardingStrategyConfiguration("id", "tableShardingAlgorithm"));

        //配置分片规则
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTables().add(orderTableRuleConfig);

        //配置分库算法
        Properties dbShardingAlgorithmProps = new Properties();
        dbShardingAlgorithmProps.setProperty("algorithm-expression", "ds_${user_id % 2}");
        shardingRuleConfig.getShardingAlgorithms().put("dbShardingAlgorithm", new
                ShardingSphereAlgorithmConfiguration("INLINE", dbShardingAlgorithmProps));

        //配置分表算法
        Properties tableShardingAlgorithmProps = new Properties();
        tableShardingAlgorithmProps.setProperty("algorithm-expression", "t_order_${id % 15}");
        shardingRuleConfig.getShardingAlgorithms().put("tableShardingAlgorithm", new
                ShardingSphereAlgorithmConfiguration("INLINE", tableShardingAlgorithmProps));

        Properties props = new Properties();
        props.put("sql-show",true);
        //创建ShardingSphereDataSource
        DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(
                dataSourceMap,
                Collections.singleton(shardingRuleConfig),
                props
        );
        return dataSource;
    }


}
